package edu.ptit.app;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.HashMap;

import edu.ptit.jad.configuration.CONSTANTS;
import edu.ptit.jad.model.Ad;
import edu.ptit.jad.model.ContentSimilarity;
import edu.ptit.jad.preprocess.analysis.TFIDFMeasure;

public class AppIFIDF {

	private static List<String> docs = new ArrayList<String>();
	private static Map<Integer, String> ads = new HashMap<Integer, String>();
	private static Map<Integer, String> news = new HashMap<Integer, String>();

	public static void main(String[] args) throws Exception {

		processFileDir("data/test/100_test_news_td.jad", news);
		processFileDir("data/test/2000_test_ads.jad", ads);

		TFIDFMeasure tfIdf = new TFIDFMeasure(
				(String[]) docs.toArray(new String[0]));

		// Map<Integer, Double> similaritySort = new HashMap<Integer, Double>();

		int numberOfDocs = news.size();

		// List<ContentSimilarity> contentSimilaritys = new
		// ArrayList<ContentSimilarity>();

		File file = new File(CONSTANTS.FILE_OUT_TFIDF_TD_SIMI);
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(file, false)));

		for (int i = 0; i < numberOfDocs; i++) {

			// System.err.println(news.get(i + 1));
			for (int j = numberOfDocs; j < docs.size(); j++)
			// System.err.println(tfIdf.getSimilarity(i, j));
			{
				// System.err.println(tfIdf.getSimilarity(i, j));
				// similaritySort.put(j + 1 - numberOfDocs,
				// tfIdf.getSimilarity(i, j));

				// contentSimilaritys.add(new ContentSimilarity(j + 1
				// - numberOfDocs, tfIdf.getSimilarity(i, j)));

				double simi = tfIdf.getSimilarity(i, j);
				out.write(simi + " ");

			}

			out.write("\n");
		}

		// System.out.println(contentSimilaritys.size());
		//
		// ContentSimilarity[] sis =
		// ContentSimilarity.getTopN(contentSimilaritys,
		// 10);
		//
		// for (ContentSimilarity si : sis) {
		//
		// System.out.print(si.getId() + "[" + si.getSimilarity() + "]"
		// + "[content:" + ads.get(si.getId()) + "]");
		// System.out.println("");
		// }

		out.close();

	}

	public static void processFileDir(String fileDir, Map<Integer, String> map) {

		// Read read file

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileDir), "UTF-8"));
			String line;

			int index = 1;
			while ((line = reader.readLine()) != null) {

				if (line.equals(""))
					continue;
				docs.add(line.trim());

				map.put(index, line);

				index++;

			}

			reader.close();

		} catch (Exception e) {
			System.out.println("Error while reading dictionary:"
					+ e.getMessage());
			e.printStackTrace();

		}

	}

	public static Map<Integer, Double> sort(Map<Integer, Double> map, int topN) {

		List<Map.Entry<Integer, Double>> terms = new ArrayList<Map.Entry<Integer, Double>>(
				map.entrySet());

		// Different terms can have the same frequency.

		Collections.sort(terms, new Comparator<Map.Entry<Integer, Double>>() {
			public int compare(Map.Entry<Integer, Double> e1,
					Map.Entry<Integer, Double> e2) {
				int result = 0;

				if (e1.getValue() < e2.getValue()) {
					result = 1; // reverse order
				} else if (e1.getValue() > e2.getValue()) {
					result = -1;
				} else {
					result = 0;
				}

				return result;
			}
		});

		Map<Integer, Double> topNMap = new HashMap<Integer, Double>();
		for (Map.Entry<Integer, Double> term : terms) {
			topNMap.put(term.getKey(), term.getValue());

			System.err.println(term.getKey() + ":" + term.getValue());
			if (topNMap.size() >= topN) {
				break;
			}
		}

		for (Map.Entry<Integer, Double> top : topNMap.entrySet()) {

			System.out.print(top.getKey() + "[" + top.getValue() + "]"
					+ "[content:" + ads.get(top.getKey()) + "]");
			System.out.println("");

		}

		return topNMap;

	}

}